package org.flower.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.flower.dao.*;
import org.flower.dao.impl.*;
import org.flower.entity.*;

@WebServlet(urlPatterns="/index.shtml")
public class indexServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);

	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//处理字符集编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		//获取登录的用户
		User u = (User)request.getSession().getAttribute("user");
		if(u!=null){
			//假如当前有用户登录
			ShoppingCartDao shdao = new ShoppingCartDaoImpl();
			//查询当前登录用户的购物车数量 并存放到session中
			int num = shdao.getShoppingCountNum(u.getId());
			request.getSession().setAttribute("userShoppingCartNum",num);
		}
		//获取导航信息
		NavigationDao navdao = new NavigationDaoImpl();
		List<Navigation> navlist = navdao.getAllNavigation();
		//获取轮播图信息
		CarouselDao cardao = new CarouselDaoImpl();
		List<Carousel> carList = null;
		try {
			carList = cardao.getCarByPage("index");
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		//获取商品类别信息
		CommodityClassDao comdao = new CommodityClassDaoImpl();
		List<CommodityClass> comlist = comdao.getCommodClassByPid(0);
		for(CommodityClass com1 : comlist){
			List<CommodityClass> comlist2 = comdao.getCommodClassByPid(com1.getId());
			com1.setSubClass(comlist2);
			for(CommodityClass com2 : comlist2){
				List<CommodityClass> comlist3 = comdao.getCommodClassByPid(com2.getId());
				com2.setSubClass(comlist3);
			}
			for(CommodityClass com1grade3 : com1.getSubClass()){
				//循环所有1级分类下的2级分类  是否有三级分类  如有：告知1级分类有对应的3级分类
				if(com1grade3.getSubClass()!=null&&com1grade3.getSubClass().size()>0){
					//进入判断说明该1级 分类中拥有3级分类
					com1.setGrade3(true);
					break;//结束本次循环
				}
			}
		}
		//查询楼层信息
		FloorDao fdao = new FloorDaoImpl();
		//1楼信息
		Floor floor1 = fdao.getFloorById(1);
		//2楼信息
		Floor floor2 = fdao.getFloorById(2);
		//3楼信息
		Floor floor3 = fdao.getFloorById(3);
		//4楼信息
		Floor floor4 = fdao.getFloorById(4);
		//5楼信息
		Floor floor5 = fdao.getFloorById(5);
		//商品展示信息
		CommodityshowDao cmds = new CommodityshowDaoImpl();
		//1楼商品
		List<Commodityshow> commShow1 = cmds.getcomdShow(1);
		//2楼商品
		List<Commodityshow> commShow2 = cmds.getcomdShow(2);
		//3楼商品
		List<Commodityshow> commShow3 = cmds.getcomdShow(3);
		//4楼商品
		List<Commodityshow> commShow4 = cmds.getcomdShow(4);
		//5楼商品
		List<Commodityshow> commShow5 = cmds.getcomdShow(5);

		//轮播图信息
		request.setAttribute("carList",carList);
		//导航栏信息
		request.setAttribute("navlist", navlist);
		//商品类别类别
		request.setAttribute("comlist", comlist);
		//楼层信息
		request.setAttribute("floor1",floor1 );
		request.setAttribute("floor2",floor2 );
		request.setAttribute("floor3",floor3 );
		request.setAttribute("floor4",floor4 );
		request.setAttribute("floor5",floor5 );
		//楼层商品详细信息
		request.setAttribute("commShow1",commShow1 );
		request.setAttribute("commShow2",commShow2 );
		request.setAttribute("commShow3",commShow3 );
		request.setAttribute("commShow4",commShow4 );
		request.setAttribute("commShow5",commShow5 );
		//页面转发
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}
